package com.tsingtree.sys.modular.system.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import com.tsingtree.base.auth.context.LoginContextHolder;
import com.tsingtree.base.auth.model.LoginUser;
import com.tsingtree.roses.kernel.model.exception.ServiceException;
import com.tsingtree.sys.core.constant.Const;
import com.tsingtree.sys.core.util.UeditorUtil;
import com.tsingtree.sys.modular.system.model.UeditorFileResult;
import com.tsingtree.sys.modular.system.model.UploadResult;
import com.tsingtree.sys.modular.system.service.FileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

import static com.tsingtree.sys.core.exception.enums.BizExceptionEnum.UE_CONFIG_ERROR;

/**
 * UEditor相关文件操作
 *
 * @author jagger
 */
@Controller
@RequestMapping("/ueditor")
@Slf4j
public class UeditorController {

    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 获取ueditor的配置
     *
     * @author jagger
     */
    @RequestMapping("/config")
    @ResponseBody
    public void getConfigInfo(HttpServletResponse response) {
        try {
            response.setContentType("application/json;charset=utf-8");
            String json = ResourceUtil.readStr("classpath:ueditor.json", StandardCharsets.UTF_8);
            response.getWriter().write(json);
            response.getWriter().flush();
        } catch (Exception e) {
            log.error("读取ueditor配置失败!", e);
            throw new ServiceException(UE_CONFIG_ERROR);
        }
    }

    /**
     * 图片上传
     *
     * @author jagger
     */
    @RequestMapping(value = "/imgUpdate")
    @ResponseBody
    public UeditorFileResult imgUpdate(@RequestParam(value = "upfile") MultipartFile upfile) {
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        if(!StringUtils.hasText(Const.OSS_BUCKET)){
            return UeditorUtil.uploadFile(upfile, UeditorUtil.FileType.IMG, currentUser.getId());
        }else{
            UploadResult uploadResult = this.fileInfoService.uploadFileOss(upfile, currentUser.getId());
            UeditorFileResult result = new UeditorFileResult();
            result.setUrl(uploadResult.getFinalName());
            result.setTitle(upfile.getOriginalFilename());
            result.setOriginal(upfile.getOriginalFilename());
            return result;
        }
    }

    /**
     * ueditor文件上传方法
     *
     * @author jagger
     */
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseBody
    public UeditorFileResult uploadFile(@RequestParam(value = "upfile") MultipartFile upfile) {
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        if(!StringUtils.hasText(Const.OSS_BUCKET)){
            return UeditorUtil.uploadFile(upfile, UeditorUtil.FileType.FILE, currentUser.getId());
        }else{
            UploadResult uploadResult = this.fileInfoService.uploadFileOss(upfile, currentUser.getId());
            UeditorFileResult result = new UeditorFileResult();
            result.setUrl(uploadResult.getFinalName());
            result.setTitle(upfile.getOriginalFilename());
            result.setOriginal(upfile.getOriginalFilename());
            return result;
        }
    }

    /**
     * 视频上传
     *
     * @author jagger
     */
    @RequestMapping(value = "/uploadvideo")
    @ResponseBody
    public UeditorFileResult uploadvideo(@RequestParam(value = "upfile") MultipartFile upfile) {
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        if(!StringUtils.hasText(Const.OSS_BUCKET)){
            return UeditorUtil.uploadFile(upfile, UeditorUtil.FileType.VIDEO, currentUser.getId());
        }else{
            UploadResult uploadResult = this.fileInfoService.uploadFileOss(upfile, currentUser.getId());
            UeditorFileResult result = new UeditorFileResult();
            result.setUrl(uploadResult.getFinalName());
            result.setTitle(upfile.getOriginalFilename());
            result.setOriginal(upfile.getOriginalFilename());
            return result;
        }
    }

}
